C++ 标准库

标准库

字符串

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <iostream>
#include <string>
#include <format>
using namespace std;

//移除字符串左侧空白符
static inline void ltrim(std::string& s) {
//从字符串左侧开始查找,找到第一个不是空白符的字符的位置
auto endPosition = std::find_if(s.begin(), s.end(), [](unsigned char ch) {
return !std::isspace(ch);
});
s.erase(s.begin(), endPosition);//移除字符串中指定位置区间的字符
}

//移除字符串右侧空白符
static inline void rtrim(std::string& s) {
//从字符串右侧开始查找,找到第一个不是空白符的字符的位置
//由于入参有两个reverse_iterator,所以最后要调用base
auto startPosition = std::find_if(s.rbegin(), s.rend(), [](unsigned char ch) {
return !std::isspace(ch);
}).base();
s.erase(startPosition, s.end());//移除字符串中指定位置区间的字符
}

//移除字符串左侧和右侧的空白符
static inline void trim(std::string& s) {
rtrim(s);
ltrim(s);
}

int main() {
auto str1{ " liulun "s }; //加了s尾缀,str1就是std:string类型,不然就是const char*
trim(str1); //c++标准库中没有trim,使用的是自己实现的。
cout << str1 << endl; //输出:liulun

int num1 = stoi("1237"s); //字符串转整型
double num2 = stod("123.45"s); //字符串转浮点型
cout << num1 << " " << num2 << endl; //输出:1237 123.45

auto str2 = to_string(num1); //数字转字符串
auto str3 = to_string(num2); //数字转字符串
cout << str2 << " " << str3 << endl; //输出:1237 123.450000

auto str4{ "他1926年8月17日出生,2022年11月30日逝世,是江苏省扬州市人。"s };
//获取子字符串,第一个参数是位置,第二个参数是数量,
cout << str4.substr(21, 19) << endl; //输出:2022年11月30日逝世
//查找子字符串的位置
cout << str4.find("2022") << endl; //输出:21
//格式化字符串
cout << format("他{0}年8月17日出生,{1}年11月30日逝世,他{0}年8月17日出生,{1}年11月30日逝世。", "1926", "2022");
//输出:他1926年8月17日出生,2022年11月30日逝世,他1926年8月17日出生,2022年11月30日逝世。
auto c = getchar();
}

正则表达式

正则入门

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>
#include <regex>
using namespace std;

//匹配手机号
void isPhoneNumber() {
//定义正则表达式,regex_constants::icase不区分大小写
regex reg{ R"(^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$)" ,regex_constants::icase };
//match是全文匹配,要求整个字符串都符合规则
bool flag = regex_match("18158135758", reg);
//输出:Is phone number:true
cout << "Is phone number:" << (flag?"true":"false") << endl;
}
//使用正则表达式替换命中的匹配项
void replaceByRegex() {
string str = "等忙完这一阵子,就可以接着忙下一阵子了。";
//一个中文字符相当于两个英文字符
regex reg{ R"(一.{2}子)" };
//输出:等忙完这一会儿,就可以接着忙下一会儿了。
cout << regex_replace(str, reg, "一会儿") << endl;
}
//使用正则表达式搜索字符串
void regexSearch() {
string str = "他1926年8月17日出生,2022年11月30日逝世,是江苏省扬州市人。";
smatch result;
regex reg{ R"(\d{4})" };
string::const_iterator iterStart = str.begin(); //这里不能用auto
string::const_iterator iterEnd = str.end(); //这里不能用auto
string temp;
//输出:1926 2022
while (regex_search(iterStart, iterEnd, result, reg))
{
cout << result[0] << " ";
//更新搜索起始位置,搜索剩下的字符
iterStart = result[0].second;
}
}

int main() {
isPhoneNumber();
replaceByRegex();
regexSearch();
auto c = getchar();
}

随机数

  • 一种是使用 srand 和 rand 方法,它的取值范围为 0 到 32767

  • 另一种是使用 random_device 类,取值范围为 0 到 4294967295

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
#include <random>
using namespace std;

//获取一定范围内的随机数
void getRangeRnd(int min, int max) {
random_device dev; //创建random_device对象
int result = (dev() % (max + 1 - min)) + min; //可以是最小值min,也可以是最大值max
cout << result << endl;
}
//使用rand方法连续获取5个随机数
void get5RandNumByRand() {
srand(time(nullptr)); //使用当前时间设置rand的随机数种子
cout << rand() << " "
<< rand() << " "
<< rand() << " "
<< rand() << " "
<< rand() << endl;
}
//使用random_device对象连续获取5个随机数
void get5RandNumByRandomDevice() {
random_device dev; //创建random_device对象
cout << dev() << " "
<< dev() << " "
<< dev() << " "
<< dev() << " "
<< dev() << " " << endl;
}
int main() {
get5RandNumByRand();
get5RandNumByRandomDevice();
getRangeRnd(100, 200);
auto c = getchar();
}

日期时间

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
#include <chrono>
using namespace std;
using namespace chrono;
int main() {
//获取当前时间 time_t为int64的值
std::time_t time = std::time(nullptr);
tm localTime;
localtime_s(&localTime, &time); //转型为本地时间
//获取当前时间,类型为system_clock:time_point
system_clock::time_point now = system_clock::now();
seconds::rep milliseconds = duration_cast<chrono::milliseconds>(now.time_since_epoch()).count() % 1000; //获取毫秒数
//打印年份,当前年份距离1900年的差值
cout << localTime.tm_year + 1900 << "-"
<< localTime.tm_mon + 1 << "-" //打印月份:tm_mon取值范围0到11
<< localTime.tm_mday << " " //打印日期:取值范围1到31
<< localTime.tm_hour << ":" //打印小时:取值范围0到23
<< localTime.tm_min << ":" //打印分钟:取值范围0到59
<< localTime.tm_sec << "." //打印秒:取值范围0到60
<< milliseconds << endl //打印毫秒:取值范围0到1000
//打印星期:取值范围0-6,星期日为0
<< "星期:" << (localTime.tm_wday == 0 ? 7 : localTime.tm_wday) << endl
//打印当前是一年中的第几天,取值范围:0到365
<< "今年的第" << localTime.tm_yday + 1 << "天" << endl;
auto a = getchar();
duration<double> span = system_clock::now() - now; //时间计算
cout << span.count() << endl; //打印时间差值:输出:4.26886
duration<double> span2 = hours(3); //表示3小时时间段
cout << span2.count() << endl; //单位秒, 输出10800
auto c = getchar();
c = getchar();
}

变体 variant

  • C++ 标准库中的 variant 可用于保存给定类型集合的一个值
  • variant 变量只能存储值、指针或 reference_wrapper 的实例
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <variant>
using namespace std;

int main() {
//变体定义时必须指定它可能包含的类型
variant<int, double, string> myVariant;
//可以把不同类型的值赋给变体变量
myVariant = 12;
myVariant = 3.1415926;
myVariant = "hello liulun";
//holds_alternative方法可用于验证变体中是否存储着指定类型的值
cout << holds_alternative<string>(myVariant) << endl; //输出 1
//index方法用于查询存储在变体中值的类型的索引,string的索引为2
cout << myVariant.index() << endl; //输出2
//根据index获取myVariant内的值,如果没有值则抛出异常
cout << get<2>(myVariant) << endl; //输出:hello liulun
//尝试根据类型获取myVariant内的值,如果没有值也不会抛出异常,
//get_if方法的输入参数是指针,返回值也是指针
string* val = get_if<string>(&myVariant);
cout << (val ? (*val) : "null") << endl; //输出:hello liulun
auto c = getchar();
}

可选变量

  • optional 变量用于保存特定类型的值或什么都不保存
  • 只能存储值、指针或 reference_wrapper 的实例
  • 可选变量与变体常用于方法的返回值
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <optional>
using namespace std;

int main() {
//默认myOptional变量内不存在值
optional<int> myOptional;
myOptional = 123;
//把myOptional设置为空,除了使用nullopt外,还可以使用myOptional = {};
myOptional = nullopt;
//判断myOptional中是否有值
cout << myOptional.has_value() << endl;//输出0
//使用value_or获取myOptional中的值,如果值不存在,则使用789
cout << myOptional.value_or(789) << endl;//输出789
myOptional = 456;
//使用value获取myOptional中的值,如果值不存在,则抛出错误
cout << myOptional.value() << endl; //输出456
//值存在,输出变量内的值,值不存在输出789
cout << myOptional.value_or(789) << endl;//输出456

auto c = getchar();
}

任意类型 any

  • C++ 标准库还提供了 any 类型用于存储任意类型的值
  • 不能在 any 变量中存储引用,只能存储值、指针或 reference_wrapper 的实例。
  • any 变量常用于函数参数
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <any>
using namespace std;

int main() {
any myAny1;
cout << myAny1.has_value() << endl; //输出0
any myAny2{ 3 };
//any变量一旦构建就已经明确了类型,不能在用其他类型为其赋值
//myAny2 = "allen"; error
//可以使用当前类型为其更改值
myAny2 = 4;
//使用any_cast获取myAny2的值
cout << myAny2.has_value() << " " //输出1
<< any_cast<int>(myAny2) << endl; //输出 4

auto c = getchar();
}
作者

大下坡

发布于

2023-11-16

更新于

2023-11-16

许可协议